/*
 * This is GPL v2 licensed code. See the file LICENSE in the
 * source distribution, or www.gnu.org/copyleft/gpl.html, for
 * more information.
 *
 * Copyright (C) by Håkan Lindqvist 2006, 2007
 */

package PolicyAlgebra.Util;

import PolicyAlgebra.Type.Policy;

import java.util.ArrayList;

/** 
 * This class is used by library to keep track of all the
 * currently loaded / created policies.
 */
public class PolicyBag extends ArrayList<Policy> {
	private static PolicyBag singleton = null;
	private static Policy latest = null;

	/** 
	 * Constructor, sets initil capacity to 10 policies.
	 */
	private PolicyBag() {
		super(10); // Default storage capacity
	}


	/** 
	 * Singleton getter method. 
	 * 
	 * @return The singleton to use in the algebra.
	 */
	public static synchronized PolicyBag getInstance() {
		if (singleton == null) {
			singleton = new PolicyBag();
		}

		return singleton;
	}


	/** 
	 * Add a new policy to the bag, keeping a reference to the lastly
	 * added.
	 * 
	 * @param p The policy to add.
	 * @return True (as per the general contract of Collection.add).
	 */
	public boolean add(Policy p) {
		super.add(p);
		latest = p;

		return true;
	}


	/** 
	 * Get the policy that was added last to the bag. 
	 * 
	 * @return The lastly added policy.
	 */
	public static Policy lastAdded() {
		return latest;
	}
}
